﻿#region Copyright

//  ======================================================
//      Copyright (c) 2011 Toulr All rights reserved.
//     
//      The use and distribution terms for this software are contained in the file
//      named license.txt, which can be found in the root of this distribution.
//      By using this software in any fashion, you are agreeing to be bound by the
//      terms of this license.
//     
//     You must not remove this notice, or any other, from this software.
//  ======================================================

#endregion

#region Using Namespaces

using System;
using System.Collections.Generic;
using System.Data.Common;
using System.Data.Entity;
using System.Data.Entity.Validation;
using System.Data.SqlClient;
using System.Diagnostics;
using NUnit.Framework;
using Toulr.Domains;
using Toulr.Domains.Account;
using Toulr.Domains.Business;
using Toulr.Domains.System;
using Toulr.Util.Configuration;

#endregion

namespace Toulr.Test.Data
{
    /// <summary>
    ///   用于完成数据库的初始化操作
    /// </summary>
    /// Author: James Cheung
    /// MachineName: JAMESCHEUNG-PC
    /// DateTime: 2011/5/10 10:15
    [TestFixture]
    public class DataBaseInit
    {
        #region Setup/Teardown

        /// <summary>
        ///   初始化当前测试环境.
        /// </summary>
        /// Author: James Cheung
        /// MachineName: JAMESCHEUNG-PC
        /// DateTime: 2011/5/10 10:10
        [SetUp]
        public void Init()
        {
            //Database.SetInitializer(new DropCreateDatabaseAlways<DomainContext>());
        }

        /// <summary>
        ///   清理当前测试环境.
        /// </summary>
        /// Author: James Cheung
        /// MachineName: JAMESCHEUNG-PC
        /// DateTime: 2011/5/10 10:10
        [TearDown]
        public void Close()
        {
        }

        #endregion

        private readonly DomainContext domainContext = DomainContext.Instance;
        
        private void InitFixedData()
        {
            /*初始化系统固定配置信息*/
            //初始化InfoCategories
            domainContext.InfoCategorieses.Add(new InfoCategories { Id = 1, CategoryName = "休闲娱乐", Enabled = true, ParentId = 0 });
            domainContext.InfoCategorieses.Add(new InfoCategories { Id = 2, CategoryName = "丽人", Enabled = true, ParentId = 0 });
            domainContext.InfoCategorieses.Add(new InfoCategories { Id = 3, CategoryName = "学习", Enabled = true, ParentId = 0 });
            domainContext.InfoCategorieses.Add(new InfoCategories { Id = 4, CategoryName = "健身", Enabled = true, ParentId = 0 });
            domainContext.InfoCategorieses.Add(new InfoCategories { Id = 5, CategoryName = "酒店", Enabled = true, ParentId = 0 });
            domainContext.InfoCategorieses.Add(new InfoCategories { Id = 6, CategoryName = "美食", Enabled = true, ParentId = 0 });
            domainContext.InfoCategorieses.Add(new InfoCategories { Id = 7, CategoryName = "景点", Enabled = true, ParentId = 0 });
            domainContext.InfoCategorieses.Add(new InfoCategories { Id = 11, CategoryName = "KTV", Enabled = true, ParentId = 1 });
            domainContext.InfoCategorieses.Add(new InfoCategories { Id = 12, CategoryName = "酒吧", Enabled = true, ParentId = 1 });


            //初始化分类扩展属性配置信息
            domainContext.InfoCategoriesPropertys.Add(new InfoCategoriesProperty { Id = 101001,TypeId = 1,DataTypeId = 1,InfoCategoriesId = 1, PropName = "特色服务" });
            domainContext.InfoCategoriesPropertys.Add(new InfoCategoriesProperty { Id = 201001, TypeId = 1, DataTypeId = 1, InfoCategoriesId = 2, PropName = "特色服务" });
            domainContext.InfoCategoriesPropertys.Add(new InfoCategoriesProperty { Id = 301001, TypeId = 1, DataTypeId = 1, InfoCategoriesId = 3, PropName = "特色服务" });
            domainContext.InfoCategoriesPropertys.Add(new InfoCategoriesProperty { Id = 401001, TypeId = 1, DataTypeId = 1, InfoCategoriesId = 4, PropName = "特色服务" });
            domainContext.InfoCategoriesPropertys.Add(new InfoCategoriesProperty { Id = 501001, TypeId = 1, DataTypeId = 1, InfoCategoriesId = 5, PropName = "特色服务" });
            domainContext.InfoCategoriesPropertys.Add(new InfoCategoriesProperty { Id = 601001, TypeId = 1, DataTypeId = 1, InfoCategoriesId = 6, PropName = "特色服务" });
            domainContext.InfoCategoriesPropertys.Add(new InfoCategoriesProperty { Id = 601002, TypeId = 1, DataTypeId = 1, InfoCategoriesId = 6, PropName = "特色菜" });

            domainContext.InfoCategoriesPropertys.Add(new InfoCategoriesProperty { Id = 102001, TypeId = 2, DataTypeId = 2, InfoCategoriesId = 1, PropName = "星级评分" });
            domainContext.InfoCategoriesPropertys.Add(new InfoCategoriesProperty { Id = 102002, TypeId = 2, DataTypeId = 2, InfoCategoriesId = 1, PropName = "环境" });
            domainContext.InfoCategoriesPropertys.Add(new InfoCategoriesProperty { Id = 102003, TypeId = 2, DataTypeId = 3, InfoCategoriesId = 1, PropName = "人均消费" });

            domainContext.InfoCategoriesPropertys.Add(new InfoCategoriesProperty { Id = 202001, TypeId = 2, DataTypeId = 2, InfoCategoriesId = 2, PropName = "星级评分" });
            domainContext.InfoCategoriesPropertys.Add(new InfoCategoriesProperty { Id = 202002, TypeId = 2, DataTypeId = 2, InfoCategoriesId = 2, PropName = "环境" });
            domainContext.InfoCategoriesPropertys.Add(new InfoCategoriesProperty { Id = 202003, TypeId = 2, DataTypeId = 3, InfoCategoriesId = 2, PropName = "人均消费" });

            domainContext.InfoCategoriesPropertys.Add(new InfoCategoriesProperty { Id = 302001, TypeId = 2, DataTypeId = 2, InfoCategoriesId = 3, PropName = "星级评分" });
            domainContext.InfoCategoriesPropertys.Add(new InfoCategoriesProperty { Id = 302002, TypeId = 2, DataTypeId = 2, InfoCategoriesId = 3, PropName = "环境" });
            domainContext.InfoCategoriesPropertys.Add(new InfoCategoriesProperty { Id = 302003, TypeId = 2, DataTypeId = 3, InfoCategoriesId = 3, PropName = "人均消费" });

            domainContext.InfoCategoriesPropertys.Add(new InfoCategoriesProperty { Id = 402001, TypeId = 2, DataTypeId = 2, InfoCategoriesId = 4, PropName = "星级评分" });
            domainContext.InfoCategoriesPropertys.Add(new InfoCategoriesProperty { Id = 402002, TypeId = 2, DataTypeId = 2, InfoCategoriesId = 4, PropName = "环境" });
            domainContext.InfoCategoriesPropertys.Add(new InfoCategoriesProperty { Id = 402003, TypeId = 2, DataTypeId = 3, InfoCategoriesId = 4, PropName = "人均消费" });

            domainContext.InfoCategoriesPropertys.Add(new InfoCategoriesProperty { Id = 502001, TypeId = 2, DataTypeId = 2, InfoCategoriesId = 5, PropName = "星级评分" });
            domainContext.InfoCategoriesPropertys.Add(new InfoCategoriesProperty { Id = 502002, TypeId = 2, DataTypeId = 2, InfoCategoriesId = 5, PropName = "环境" });
            domainContext.InfoCategoriesPropertys.Add(new InfoCategoriesProperty { Id = 502003, TypeId = 2, DataTypeId = 3, InfoCategoriesId = 5, PropName = "人均消费" });

            domainContext.InfoCategoriesPropertys.Add(new InfoCategoriesProperty { Id = 602001, TypeId = 2, DataTypeId = 2, InfoCategoriesId = 6, PropName = "星级评分" });
            domainContext.InfoCategoriesPropertys.Add(new InfoCategoriesProperty { Id = 602002, TypeId = 2, DataTypeId = 2, InfoCategoriesId = 6, PropName = "环境" });
            domainContext.InfoCategoriesPropertys.Add(new InfoCategoriesProperty { Id = 602003, TypeId = 2, DataTypeId = 2, InfoCategoriesId = 6, PropName = "上菜速度" });
            domainContext.InfoCategoriesPropertys.Add(new InfoCategoriesProperty { Id = 602004, TypeId = 2, DataTypeId = 2, InfoCategoriesId = 6, PropName = "口味" });
            domainContext.InfoCategoriesPropertys.Add(new InfoCategoriesProperty { Id = 602005, TypeId = 2, DataTypeId = 3, InfoCategoriesId = 6, PropName = "人均消费" });
            domainContext.InfoCategoriesPropertys.Add(new InfoCategoriesProperty { Id = 602006, TypeId = 2, DataTypeId = 2, InfoCategoriesId = 6, PropName = "服务态度" });
            //初始化省份信息
            domainContext.Provinces.Add(new Province
                                             {
                                                 Id = 1,
                                                 ProvinceName = "北京",
                                                 Enabled = true,
                                             });

            domainContext.Provinces.Add(new Province
                                             {
                                                 Id = 2,
                                                 ProvinceName = "天津",
                                                 Enabled = true,
                                             });


            //初始化城市信息
            domainContext.Cities.Add(new City
                                          {
                                              CityName = "北京",
                                              Enabled = true,
                                              Id = 101,
                                              ProvinceId = 1
                                          });

            domainContext.Cities.Add(new City
                                          {
                                              CityName = "天津",
                                              Enabled = true,
                                              Id = 201,
                                              ProvinceId = 2
                                          });

            //初始化行政区信息
            domainContext.Counties.Add(new Counties
                                            {
                                                Id = 10101,
                                                CountiesName = "朝阳",
                                                Enabled = true,
                                                CityId = 101
                                            });
            domainContext.Counties.Add(new Counties
                                            {
                                                Id = 10102,
                                                CountiesName = "海淀",
                                                Enabled = true,
                                                CityId = 101
                                            });
            domainContext.Counties.Add(new Counties
                                            {
                                                Id = 10103,
                                                CountiesName = "东城",
                                                Enabled = true,
                                                CityId = 101
                                            });
            domainContext.Counties.Add(new Counties
                                            {
                                                Id = 10104,
                                                CountiesName = "西城",
                                                Enabled = true,
                                                CityId = 101
                                            });
            domainContext.Counties.Add(new Counties
                                            {
                                                Id = 10105,
                                                CountiesName = "崇文",
                                                Enabled = true,
                                                CityId = 101
                                            });
            domainContext.Counties.Add(new Counties
                                            {
                                                Id = 10106,
                                                CountiesName = "宣武",
                                                Enabled = true,
                                                CityId = 101
                                            });
            domainContext.Counties.Add(new Counties
                                            {
                                                Id = 10107,
                                                CountiesName = "丰台",
                                                Enabled = true,
                                                CityId = 101
                                            });
            domainContext.Counties.Add(new Counties
                                            {
                                                Id = 10108,
                                                CountiesName = "石景山",
                                                Enabled = true,
                                                CityId = 101
                                            });
            domainContext.Counties.Add(new Counties
                                            {
                                                Id = 10109,
                                                CountiesName = "昌平",
                                                Enabled = true,
                                                CityId = 101
                                            });
            domainContext.Counties.Add(new Counties
                                            {
                                                Id = 10110,
                                                CountiesName = "通州",
                                                Enabled = true,
                                                CityId = 101
                                            });
            domainContext.Counties.Add(new Counties
                                            {
                                                Id = 10111,
                                                CountiesName = "大兴",
                                                Enabled = true,
                                                CityId = 101
                                            });
            domainContext.Counties.Add(new Counties
                                            {
                                                Id = 10112,
                                                CountiesName = "顺义",
                                                Enabled = true,
                                                CityId = 101
                                            });
            domainContext.Counties.Add(new Counties
                                            {
                                                Id = 10113,
                                                CountiesName = "房山",
                                                Enabled = true,
                                                CityId = 101
                                            });
            domainContext.Counties.Add(new Counties
                                            {
                                                Id = 10114,
                                                CountiesName = "密云",
                                                Enabled = true,
                                                CityId = 101
                                            });
            domainContext.Counties.Add(new Counties
                                            {
                                                Id = 10115,
                                                CountiesName = "门头沟",
                                                Enabled = true,
                                                CityId = 101
                                            });
            domainContext.Counties.Add(new Counties
                                            {
                                                Id = 10116,
                                                CountiesName = "怀柔",
                                                Enabled = true,
                                                CityId = 101
                                            });
            domainContext.Counties.Add(new Counties
                                            {
                                                Id = 10117,
                                                CountiesName = "平谷",
                                                Enabled = true,
                                                CityId = 101
                                            });
            domainContext.Counties.Add(new Counties
                                            {
                                                Id = 10118,
                                                CountiesName = "延庆",
                                                Enabled = true,
                                                CityId = 101
                                            });
            domainContext.Counties.Add(new Counties
                                            {
                                                Id = 20101,
                                                CountiesName = "和平区",
                                                Enabled = true,
                                                CityId = 201
                                            });
            domainContext.Counties.Add(new Counties
                                            {
                                                Id = 20102,
                                                CountiesName = "河西区",
                                                Enabled = true,
                                                CityId = 201
                                            });
            domainContext.Counties.Add(new Counties
                                            {
                                                Id = 20103,
                                                CountiesName = "红桥区",
                                                Enabled = true,
                                                CityId = 201
                                            });
            domainContext.Counties.Add(new Counties
                                            {
                                                Id = 20104,
                                                CountiesName = "汉沽区",
                                                Enabled = true,
                                                CityId = 201
                                            });
            domainContext.Counties.Add(new Counties
                                            {
                                                Id = 20105,
                                                CountiesName = "大港区",
                                                Enabled = true,
                                                CityId = 201
                                            });
            domainContext.Counties.Add(new Counties
                                            {
                                                Id = 20106,
                                                CountiesName = "东丽区",
                                                Enabled = true,
                                                CityId = 201
                                            });
            domainContext.Counties.Add(new Counties
                                            {
                                                Id = 20107,
                                                CountiesName = "西青区",
                                                Enabled = true,
                                                CityId = 201
                                            });
            domainContext.Counties.Add(new Counties
                                            {
                                                Id = 20108,
                                                CountiesName = "津南区",
                                                Enabled = true,
                                                CityId = 201
                                            });
            domainContext.Counties.Add(new Counties
                                            {
                                                Id = 20109,
                                                CountiesName = "北辰区",
                                                Enabled = true,
                                                CityId = 201
                                            });
            domainContext.Counties.Add(new Counties
                                            {
                                                Id = 20110,
                                                CountiesName = "武清区",
                                                Enabled = true,
                                                CityId = 201
                                            });
            domainContext.Counties.Add(new Counties
                                            {
                                                Id = 20111,
                                                CountiesName = "宝坻区",
                                                Enabled = true,
                                                CityId = 201
                                            });
            domainContext.Counties.Add(new Counties
                                            {
                                                Id = 20112,
                                                CountiesName = "蓟县",
                                                Enabled = true,
                                                CityId = 201
                                            });
            domainContext.Counties.Add(new Counties
                                            {
                                                Id = 20113,
                                                CountiesName = "宁河县",
                                                Enabled = true,
                                                CityId = 201
                                            });
            domainContext.Counties.Add(new Counties
                                            {
                                                Id = 20114,
                                                CountiesName = "静海县",
                                                Enabled = true,
                                                CityId = 201
                                            });


            //初始化用户爱好信息
            domainContext.UserHobbies.Add(new UserHobby { Id = 1, HobbyName = "读书" });
            domainContext.UserHobbies.Add(new UserHobby { Id = 2, HobbyName = "旅游" });
            domainContext.UserHobbies.Add(new UserHobby { Id = 3, HobbyName = "篮球" });
            domainContext.UserHobbies.Add(new UserHobby { Id = 4, HobbyName = "足球" });
            domainContext.UserHobbies.Add(new UserHobby { Id = 5, HobbyName = "购物" });
            domainContext.UserHobbies.Add(new UserHobby { Id = 6, HobbyName = "电子产品" });
            domainContext.UserHobbies.Add(new UserHobby { Id = 13, HobbyName = "化妆品" });
            //初始化快递公司信息
            domainContext.ExpressDeliveries.Add(new ExpressDelivery
                                                     {
                                                         Id = 1,
                                                         Description = "圆通快递",
                                                         ExpressDeliveryService = "#",
                                                         Name = "圆通快递"
                                                     });
            domainContext.ExpressDeliveries.Add(new ExpressDelivery
                                                     {
                                                         Id = 2,
                                                         Description = "申通快递",
                                                         ExpressDeliveryService = "#",
                                                         Name = "申通快递",
                                                         Enabled = true
                                                     });
            domainContext.ExpressDeliveries.Add(new ExpressDelivery
                                                     {
                                                         Id = 3,
                                                         Description = "EMS",
                                                         ExpressDeliveryService = "#",
                                                         Name = "EMS",
                                                         Enabled = true
                                                     });
            //初始化订单状态配置信息
            domainContext.OrderStatus.Add(new OrderStatus { Id = 1, StatusName = "待确认" });
            domainContext.OrderStatus.Add(new OrderStatus { Id = 2, StatusName = "已确认，等待付款" });
            domainContext.OrderStatus.Add(new OrderStatus { Id = 3, StatusName = "已付款，等待发货" });
            domainContext.OrderStatus.Add(new OrderStatus { Id = 4, StatusName = "已发货，等待确认收货" });
            domainContext.OrderStatus.Add(new OrderStatus { Id = 5, StatusName = "已完成" });
            //加入朋友状态配置
            domainContext.FriendStatus.Add(new FriendStatus { Id = 0, StatusName = "未确认" });
            domainContext.FriendStatus.Add(new FriendStatus { Id = 1, StatusName = "已确认" });
            domainContext.FriendStatus.Add(new FriendStatus { Id = 2, StatusName = "已拒绝" });
            domainContext.FriendStatus.Add(new FriendStatus { Id = 3, StatusName = "已删除" });

            //初始化角色信息
            var role = new UserRole { Enable = true, RoleName = "普通用户", Id = 1 };
            var roleShopKeeper = new UserRole { Enable = true, RoleName = "店长", Id = 2 };
            var roleManager = new UserRole { Enable = true, RoleName = "店主", Id = 3 };
            var roleAdmin = new UserRole { Enable = true, RoleName = "管理员", Id = 0 };
            domainContext.UserRoles.Add(roleAdmin);
            domainContext.UserRoles.Add(roleShopKeeper);
            domainContext.UserRoles.Add(roleManager);
            domainContext.UserRoles.Add(role);

            //角色权限关联信息
            var permissionss = new List<Permission>
                                   {
                                       new Permission
                                           {
                                               Enable = true,
                                               PermissionCode = "just for test",
                                               PermissionName = "登录",
                                               Id = 0
                                           },
                                       new Permission
                                           {
                                               Enable = true,
                                               PermissionCode = "just for test",
                                               PermissionName = "购买",
                                               Id = 1
                                           },
                                       new Permission
                                           {
                                               Enable = true,
                                               PermissionCode = "just for test",
                                               PermissionName = "评论",
                                               Id = 2
                                           },
                                       new Permission
                                           {
                                               Enable = true,
                                               PermissionCode = "just for test",
                                               PermissionName = "发起团购",
                                               Id = 3
                                           },
                                       new Permission
                                           {
                                               Enable = true,
                                               PermissionCode = "just for test",
                                               PermissionName = "浏览",
                                               Id = 4
                                           }
                                   };

            foreach (var permission in permissionss)
            {
                domainContext.Permissions.Add(permission);
                domainContext.RolePermissions.Add(new RolePermissions
                                                       {
                                                           RoleId = role.Id,
                                                           PermissionsId = permission.Id
                                                       });
            }

            //初始化商圈信息

            domainContext.ShoppingAreas.Add(new ShoppingArea { AreaName = "亚奥商圈", CityId =domainContext.Cities.Find(101).Id, Enable = true, Id = 101001 });
            domainContext.ShoppingAreas.Add(new ShoppingArea { AreaName = "上地商圈", CityId = domainContext.Cities.Find(101).Id, Enable = true, Id = 101002 });
            domainContext.ShoppingAreas.Add(new ShoppingArea { AreaName = "中关村商圈", CityId = domainContext.Cities.Find(101).Id, Enable = true, Id = 101003 });
            domainContext.ShoppingAreas.Add(new ShoppingArea { AreaName = "西单商圈", CityId = domainContext.Cities.Find(101).Id, Enable = true, Id = 101004 });
            domainContext.ShoppingAreas.Add(new ShoppingArea { AreaName = "王府井商圈", CityId = domainContext.Cities.Find(101).Id, Enable = true, Id = 101005 });
            domainContext.ShoppingAreas.Add(new ShoppingArea { AreaName = "CBD商圈", CityId = domainContext.Cities.Find(101).Id, Enable = true, Id = 101006 });

            //初始化用户信息
            domainContext.Users.Add(new User
                                        {
                                            BirthDay = 29,
                                            BirthMonth = 1,
                                            BirthPlace = "大庆",
                                            BirthYear = 1986,
                                            Email = "hy20110509@gmail.com",
                                            EmailVerified = true,
                                            FullName = "于海洋",
                                            Identitied = true,
                                            IdentityId = "230604198601293011",
                                            IdentityType = 1,
                                            UserCityId = 101,
                                            NickName = "神仙",
                                            SaltValue = 1,
                                            RegisterTime = DateTime.Now
                                        });

            domainContext.SaveChanges();
        }

        /// <summary>
        ///   测试到当前数据库连接串指定连接是否正常
        /// </summary>
        [Test]
        public void DbConnectTest()
        {
            var connectString = ConfigurationHelper.GetConnectionString("ToulrDb");
            try
            {
                DbConnection connection =
                    new SqlConnection(connectString);
                connection.Open();
                connection.Close();
                Assert.Pass("到数据库的链接正常！！");
            }
            catch (DbException ex)
            {
                Debug.Print(ex.Message);
                Assert.Fail("到数据库的链接失败:" + connectString);
            }
        }

        [Test]
        public void TestDataInit()
        {
            try
            {
                domainContext.SaveChanges();


                InitFixedData();
                Assert.Pass("OK");
            }
            catch (DbEntityValidationException ex)
            {
                foreach (var dbEntityValidationResult in ex.EntityValidationErrors)
                {
                    Debug.WriteLine(dbEntityValidationResult.ValidationErrors);
                }
                Assert.Fail(ex.Message);
                
            }
            catch (DbException ex)
            {
                Debug.WriteLine(ex.Message);
                Assert.Fail(ex.Message);
              
            }
        }
    }
}